
using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore.Repositories;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.Integratedes.Organization.DomainService
{
	/// <summary>
	/// 领域服务层一个模块的核心业务逻辑
	///</summary>
	public class OEconomyOrganiCancelManager :DomainServiceBase, IOEconomyOrganiCancelManager
	{
		
		private readonly IRepository<OEconomyOrganiCancel,string> _oeconomyorganicancelRepository;

		/// <summary>
		/// 【OEconomyOrganiCancel】的构造方法
		/// 通过构造函数注册服务到依赖注入容器中
		///</summary>
		public OEconomyOrganiCancelManager(IRepository<OEconomyOrganiCancel, string> oeconomyorganicancelRepository)	{
			_oeconomyorganicancelRepository =  oeconomyorganicancelRepository;
		}

		#region -------------------------------------------------辅助工具生成---------------------------------------------- 

		/// <summary>
		/// 返回列表查询用
		/// </summary>
		/// <returns></returns>
		public IQueryable<OEconomyOrganiCancel> QueryEntityListAsNoTracking() { 

			var query = _oeconomyorganicancelRepository.GetAll().AsNoTracking()
						.Select(x => new OEconomyOrganiCancel
						{
						   
							Id = x.Id,
							JJJGBH = x.JJJGBH,
							ZXRBH = x.ZXRBH,
							ZXRMC = x.ZXRMC,
							ZXYY = x.ZXYY,
							ZXSJ = x.ZXSJ,
						});
			return query;
		}

		/// <summary>
		/// 【OEconomyOrganiCancel】返回表达式数的实体信息即IQueryable类型
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<OEconomyOrganiCancel> Query()
		{
			return _oeconomyorganicancelRepository.GetAll();
		}
		/// <summary>
		/// 【OEconomyOrganiCancel】返回即IQueryable类型的实体，不包含EF Core跟踪标记
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<OEconomyOrganiCancel> QueryAsNoTracking()
		{
			return _oeconomyorganicancelRepository.GetAll().AsNoTracking();
		}
		/// <summary>
		/// 【OEconomyOrganiCancel】根据Id查询实体信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns>实体</returns>
		public async Task<OEconomyOrganiCancel> FindByIdAsync(string id)
		{
			var entity = await _oeconomyorganicancelRepository.GetAsync(id);
			return entity;
		}
		/// <summary>
		/// 【OEconomyOrganiCancel】检查实体是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <returns>bool</returns>
		public async Task<bool> IsExistAsync(string id)
		{
			var result = await _oeconomyorganicancelRepository.GetAll().AnyAsync(a => a.Id == id);
			return result;
		}
		/// <summary>
		/// 【OEconomyOrganiCancel】创建实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<OEconomyOrganiCancel> CreateAsync(OEconomyOrganiCancel entity)
		{
			entity.Id = this.GetNewSequenceCode(); // 创建会自动设置序列
			entity.Id = await _oeconomyorganicancelRepository.InsertAndGetIdAsync(entity);
			return entity;
		}
		/// <summary>
		/// 【OEconomyOrganiCancel】更新实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task UpdateAsync(OEconomyOrganiCancel entity)
		{
			await _oeconomyorganicancelRepository.UpdateAsync(entity);
		}
		/// <summary>
		/// 【OEconomyOrganiCancel】删除实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task DeleteAsync(string id)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _oeconomyorganicancelRepository.DeleteAsync(id);
		}
		/// <summary>
		/// 【OEconomyOrganiCancel】批量删除
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task BatchDelete(List<string> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _oeconomyorganicancelRepository.DeleteAsync(a => input.Contains(a.Id));
		}
		#endregion


		#region -------------------------------------------------用户自定义------------------------------------------------

		/*请在此扩展领域服务接口*/

		/// <summary>
		/// 是否存在指定编号的经济机构
		/// </summary>
		/// <param name="OrgniID">经济机构编号</param>
		/// <returns></returns>
		public async Task<bool> IsExistCancelAsync(string OrgniID)
		{
			return await this.Query().AnyAsync(x => x.JJJGBH == OrgniID);
		}

		/// <summary>
		/// 【原始系统】获取机构人员编号
		/// </summary>
		/// <returns></returns>
		public object GetSequence()
		{
			var sql = string.Format("select {0}.Nextval from dual", "SEQ_JJJGZX");
			return _oeconomyorganicancelRepository.GetDbContext().Database.ExecuteScalar(sql);
		}

		/// <summary>
		/// 【原始系统】获取新的人员编号
		/// </summary>
		/// <returns></returns>
		public string GetNewSequenceCode()
		{
			var code = this.GetSequence().ToString();
			return code.PadLeft(10, '0');
		}

		#endregion
	}
}
